import axios from 'axios'

// 创建axios实例
const instance = axios.create({
    baseURL: 'http://localhost:8081',  // 后端服务器地址
    timeout: 5000,  // 请求超时时间
    headers: {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
        'Access-Control-Allow-Headers': '*'
    }
})

// 请求拦截器
instance.interceptors.request.use(
    config => {
        // 在发送请求之前做些什么
        return config
    },
    error => {
        // 对请求错误做些什么
        return Promise.reject(error)
    }
)

// 响应拦截器
instance.interceptors.response.use(
    response => {
        // 对响应数据做点什么
        return response.data
    },
    error => {
        // 对响应错误做点什么
        return Promise.reject(error)
    }
)

//API请求函数
export default {

    //获取员工列表（分页）
    getEmployees(page = 1, size = 8) {
        return instance.get('/database/viewEmployees', {
            params: { page, size }
        })
    },

    // 根据日期范围筛选员工
    filterEmployeesByDate(startDate, endDate, page = 1, size = 8) {
        return instance.get('/database/filterByDate', {
            params: { startDate, endDate, page, size }
        })
    },

    //添加员工
    addEmployee(employee) {
        return instance.post('/database/addEmployee', employee)
    },

    //更新员工信息
    updateEmployee(employee) {
        return instance.post('/database/updateEmployee', employee)
    },

    //删除员工
    deleteEmployee(employeeId) {
        return instance.delete(`/database/deleteEmployee/${employeeId}`)
    },

    // 批量删除员工
    deleteEmployees(employeeIds) {
        return instance.post('/database/deleteEmployees', { employeeIds })
    },

    // 导入Excel文件
    importExcel(file) {
        const formData = new FormData()
        formData.append('file', file)
        return instance.post('/database/import', formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
    },

    // 导出Excel文件
    exportExcel() {
        return instance.get('/database/export', {
            responseType: 'blob'
        })
    },

    // 下载Excel模板
    downloadTemplate() {
        return instance.get('/database/downloadTemplate', {
            responseType: 'blob'
        })
    },

    // 查看填报表数据 (带分页)
    viewEmployeesSubmit(page = 1, size = 8) {
        return instance.get('/database/viewEmployeesSubmit', {
            params: { page, size }
        });
    },

    // 根据日期查看填报表数据 (带分页)
    viewEmployeesSubmitByDate(date, page = 1, size = 8) {
        return instance.get('/database/viewEmployeesSubmitByDate', {
            params: { date, page, size }
        });
    },

    // 数据转换
    convertData() {
        return instance.get('/database/convert')
    }
} 